package com.ten.shou.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ten.common.core.entity.EduCourse;
import com.ten.common.core.entity.TbZoneCourse;
import com.ten.common.core.vo.Result;
import com.ten.shou.dao.EduCourseDao;
import com.ten.shou.dao.ZoneCourseDao;
import com.ten.shou.dao.ZoneDao;
import com.ten.shou.feign.CourseListFeign;
import com.ten.shou.service.ZoneCourseService;
import com.ten.shou.vo.CourseCheckVo;
import com.ten.shou.vo.CourseVo;
import com.ten.shou.vo.EduCourseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：杨（1964958771@qq.com）
 * @date ：Created in 2022/12/7 17:58
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ZoneCourseServiceImpl extends ServiceImpl<ZoneCourseDao, TbZoneCourse> implements ZoneCourseService {
    @Resource
    private ZoneDao zoneDao;
    @Resource
    private ZoneCourseDao zoneCourseDao;
    @Resource
    private EduCourseDao eduCourseDao;
    @Resource
    private CourseListFeign courseListFeign;

    @Override
    public Result getAllZoneCourse(Integer current, Integer pageSize, String id, CourseVo courseVo) {
        IPage<EduCourseVo> page = new Page<>(current, pageSize);
        List<EduCourseVo> allCourse = zoneDao.getAllCourse(page, id, courseVo.getTitle(), courseVo.getIsDeleted());
        Result<Page<EduCourse>> result = courseListFeign.findCourse(current, pageSize, new CourseCheckVo());
        List<EduCourse> records = result.getData().getRecords();

        for (EduCourseVo eduCourseVo : allCourse) {
            for (EduCourse record : records) {
                if (record.getId().equals(eduCourseVo.getCourseId())) {
                    String subjectName = record.getSubjectName();
                    eduCourseVo.setSubjectName(subjectName);
                }
            }
        }
        return new Result(2000, "查询成功", allCourse);
    }

    @Override
    public Result findCourse(Integer pageCurrent, Integer pageSize, CourseCheckVo courseCheckVo) {
        //调用课程远程微服务，获取可以进行添加到专区的课程信息
        return courseListFeign.getAllDetail(pageCurrent, pageSize, courseCheckVo);
    }

    @Override
    @Transactional
    public Result addCourseDetail(TbZoneCourse tbZoneCourse) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setIsAdd(1);
        eduCourse.setId(tbZoneCourse.getCourseId());
        Result result = courseListFeign.updateIsAdd(eduCourse);
        if (result.getCode() == 200) {
            tbZoneCourse.setIsDeleted(0);
            tbZoneCourse.setIsDisable("0");
            int res = zoneCourseDao.insert(tbZoneCourse);
            if (res > 0) {
                return new Result(2000, "添加成功", true);
            }
        }
        return new Result(5000, "添加失败", false);

    }

    @Override
    public Result<List<EduCourse>> getCourseByZoneId(String zoneId) {
        List<EduCourse> courseByZoneId = eduCourseDao.getCourseByZoneId(zoneId);
        return new Result<>(2000, "", courseByZoneId);
    }

    @Override
    public Result deleteZoneCourse(TbZoneCourse tbZoneCourse) {
        TbZoneCourse tbZoneCourse1 = zoneCourseDao.selectById(tbZoneCourse.getId());
        EduCourse eduCourse = new EduCourse();
        eduCourse.setIsAdd(0);
        eduCourse.setId(tbZoneCourse1.getCourseId());
        Result result = courseListFeign.updateIsAdd(eduCourse);
        if (result.getCode() == 200) {
            int i = zoneCourseDao.deleteById(tbZoneCourse.getId());
            if (i > 0) {
                return new Result(2000, "删除成功", true);
            }
        }
        return new Result(5000, "删除失败", false);
    }

//    @Transactional
//    @Override
//    public Result deleteZoneCourse(EduCourse eduCourse) {
//        eduCourse.setIsAdd(0);
//        Result result = courseListFeign.updateIsAdd(eduCourse);
//        System.out.println(result.getCode());
//        if (result.getCode()==200){
//            int i = zoneCourseDao.deleteByCourseId(eduCourse.getId());
//            if (i>0){
//                return new Result(2000,"删除成功",true);
//            }
//        }
//        return new Result(5000,"删除失败",false);
//    }

}
